part of 'browser_page.dart';


class FlutterBridge {
  FlutterBridge(this._context, this._onBackPressed, {this.onClosePressed, this.hideAccUsePressed});

  final BuildContext _context;
  final Function _onBackPressed;
  final Function? onClosePressed;
  final Function? hideAccUsePressed;
  late final InAppWebViewController _controller;

  void addJavaScriptHandler(InAppWebViewController controller) {
    _controller = controller;
    controller.addJavaScriptHandler(
        handlerName: "closeCurrentPage",
        callback: (arguments) {
          debugPrint("closeCurrentPage");
          onClosePressed?.call();
        });
    controller.addJavaScriptHandler(
        handlerName: "hideAccUse",
        callback: (arguments) {
          debugPrint("hideAccUse");
          hideAccUsePressed?.call();
        });
    controller.addJavaScriptHandler(
        handlerName: "samplePicture",
        callback: (arguments) {
          debugPrint("samplePicture.callback():$arguments");
        });
    controller.addJavaScriptHandler(
        handlerName: "webRequest",
        callback: (arguments) {
          debugPrint("webRequest.callback():$arguments");
          _webRequest(arguments[0]);
        });
    controller.addJavaScriptHandler(
        handlerName: "mobileRedirect",
        callback: (arguments) {
          debugPrint("mobileRedirect.callback():$arguments");
        });
    controller.addJavaScriptHandler(
        handlerName: "goRouter",
        callback: (arguments) {
          debugPrint("goRouter.callback():$arguments");
        });
    controller.addJavaScriptHandler(
        handlerName: "mobileBack",
        callback: (arguments) {
          debugPrint("FlutterBridge.mobileBack():$arguments");
          _onBackPressed.call();
        });
    controller.addJavaScriptHandler(
        handlerName: "pushDeviceMessage",
        callback: (arguments) {
          debugPrint("FlutterBridge.pushDeviceMessage() arguments$arguments");
        });
    controller.addJavaScriptHandler(
        handlerName: "qyUserInfo",
        callback: (arguments) {
          debugPrint("FlutterBridge.qyUserInfo() arguments$arguments");
          _qyUserInfo(arguments[0]);
        });
  }

  Future<void> _qyUserInfo(Map<String, dynamic> info) async {
    var callback = info["callback"];
    var order = QiyuOrder();
    var data = {"uid": "6000000000", "data": order.data.toString()};
    String source = "$callback(${jsonEncode(data)})";
    debugPrint("FlutterBridge.qyUserInfo() source:$source");
    _controller.evaluateJavascript(source: source);
  }

  String _desensitization(String text) {
    if (text.length >= 3) {
      return "${text.substring(0, 2)}****${text.substring(text.length - 1)}";
    } else {
      return text;
    }
  }

  Future<void> _webRequest(Map<String, dynamic> info) async {
    String? type = info["type"];
    String url = info["url"] ?? "";
    Map<String, dynamic> params = info["params"] ?? {};
    String callBack = info["callback"] ?? "";
    ApiResponse response;
    if (type == "POST") {
      response = await Https.instance.post(url, params: params);
    } else {
      response = await Https.instance.get(url, params: params);
    }
    if (response.statusCode == 200) {
      var data = {"http_code": 200, "result": response.response};
      String actionStr = "$callBack(${jsonEncode(data)})";
      debugPrint("FlutterBridge.webRequest() actionStr:$actionStr");
      _controller.evaluateJavascript(source: actionStr);
    } else {
      var data = {"http_code": 202, "message": response.errorDesc};
      String actionStr = "$callBack(${jsonEncode(data)})";
      debugPrint("FlutterBridge.webRequest() actionStr:$actionStr");
      _controller.evaluateJavascript(source: actionStr);
    }
  }
}
